﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_RobotQuotation : UserControl
    {
        public panel_RobotQuotation()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.RobotQuotation.PopupMenuShowing += RobotQuotation_PopupMenuShowing;
            this.robotQuotationContextMenu.ItemClicked += robotQuotationContextMenu_ItemClicked;
            this.RobotQuotation.DoubleClick += RobotQuotation_DoubleClick;
        }

        //////////////////////////////////////////
        /// Call Edit Form
        ///////////////////////////////////////////
        private void callEditForm()
        {
            /*
            DataRow row = RobotQuotation.GetFocusedDataRow();
            frm_Edit_RobotQuotation edit_form = new frm_Edit_RobotQuotation();
            edit_form.id = Int32.Parse(row["ID"].ToString());
            if (edit_form.LoadData())
            {
                edit_form.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Cannot get data from Quotation list");
            } */
        }

        void RobotQuotation_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();
        }

        void robotQuotationContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.robotQuotationContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "changeSelection":
                    callEditForm();                    
                    break;
                case "addnewSelection":
                    frm_Add_RobotQuotation add_form = new frm_Add_RobotQuotation();
                    add_form.ShowDialog();
                    LoadData();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void RobotQuotation_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.robotQuotationContextMenu.Show(MousePosition);            
        }       

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_RobotQuotation add_form = new frm_Add_RobotQuotation();
            add_form.ShowDialog();
            LoadData();
        }

        
        /////////////////////////////////////////
        /// Load Export Robot Plan into gridview
        /////////////////////////////////////////
        private void LoadData()
        {            
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [241_RobotPrice].ID, [241_RobotPrice].QuotationID, [241_RobotPrice].CompID,"
                + " [241_RobotPrice].Quantity1, [241_RobotPrice].Quantity2, [241_RobotPrice].Quantity3, [241_RobotPrice].Quantity4,"
                + " [241_RobotPrice].Quantity5, [241_RobotPrice].Price1, [241_RobotPrice].Price2, [241_RobotPrice].Price3,"
                + " [241_RobotPrice].Price4, [241_RobotPrice].Price5, [222_Currency].CurrencySymbol, [222_DeliveryTerm].DeliveryTermName,"
                + " [241_RobotPrice].Note"
                + " FROM [241_RobotPrice] LEFT OUTER JOIN"
                + " [222_DeliveryTerm] ON [241_RobotPrice].PriceType=[222_DeliveryTerm].DeliveryTermID LEFT OUTER JOIN"
                + " [241_ExportQuotation] ON [241_RobotPrice].QuotationID=[241_ExportQuotation].QuotationID LEFT OUTER JOIN"
                + " [222_Currency] ON [241_ExportQuotation].CurrencyID=[222_Currency].CurrencyID"
                + " ORDER BY [241_RobotPrice].QuotationID, [241_RobotPrice].CompID";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridRobotQuotation.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Robot Quotation");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("QuotationID", typeof(string));
                newTable.Columns.Add("Comp ID", typeof(string));
                newTable.Columns.Add("Quantity1", typeof(int));
                newTable.Columns.Add("Quantity2", typeof(int));
                newTable.Columns.Add("Quantity3", typeof(int));
                newTable.Columns.Add("Quantity4", typeof(int));
                newTable.Columns.Add("Quantity5", typeof(int));
                newTable.Columns.Add("Price1", typeof(float));
                newTable.Columns.Add("Price2", typeof(float));
                newTable.Columns.Add("Price3", typeof(float));
                newTable.Columns.Add("Price4", typeof(float));
                newTable.Columns.Add("Price5", typeof(float));
                newTable.Columns.Add("Currency", typeof(string));
                newTable.Columns.Add("PriceType", typeof(int));
                newTable.Columns.Add("Note", typeof(string));
                gridRobotQuotation.DataSource = newTable;                
            }
            RobotQuotation.Columns[0].Caption = "ID";
            RobotQuotation.Columns[1].Caption = "Quotation ID";
            RobotQuotation.Columns[2].Caption = "Robot Name";
            RobotQuotation.Columns[3].Caption = "Quantity 1";
            RobotQuotation.Columns[4].Caption = "Quantity 2";
            RobotQuotation.Columns[5].Caption = "Quantity 3";
            RobotQuotation.Columns[6].Caption = "Quantity 4";
            RobotQuotation.Columns[7].Caption = "Quantity 5";
            RobotQuotation.Columns[8].Caption = "Price 1";
            RobotQuotation.Columns[9].Caption = "Price 2";
            RobotQuotation.Columns[10].Caption = "Price 3";
            RobotQuotation.Columns[11].Caption = "Price 4";
            RobotQuotation.Columns[12].Caption = "Price 5";
            RobotQuotation.Columns[13].Caption = "Currency";
            RobotQuotation.Columns[14].Caption = "Price Type";
            RobotQuotation.Columns[15].Caption = "Note";
            RobotQuotation.BestFitColumns();
            RobotQuotation.Columns[0].Visible = false;
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }
    }
}
